<template>
  <div class="app-container">
    <el-form
      ref="form"
      v-loading="infoLoading"
      class="common-form"
      :model="form"
      label-width="190px"
    >
      <el-form-item label="商户号(MCHID)">
        <el-input v-model="form.payment_wechat_mchid" class="middle" />
      </el-form-item>
      <el-form-item label="商户支付密钥(KEY)">
        <el-input v-model="form.payment_wechat_key" class="middle" />
      </el-form-item>
      <el-form-item label="绑定支付的APPID">
        <el-input v-model="form.payment_wechat_appid" class="middle" />
      </el-form-item>
      <el-form-item label="公众帐号secert">
        <el-input v-model="form.payment_wechat_appsecret" class="middle" />
      </el-form-item>
      <el-form-item label="APP(ios/android)appid">
        <el-input v-model="form.payment_wechat_app_appid" class="middle" />
        <span class="smalltip">
          <i class="el-icon-info" />
          不使用app支付可不填
        </span>
      </el-form-item>
      <el-form-item label="APP(ios/android)appsecret">
        <el-input v-model="form.payment_wechat_app_app_appsecret" class="middle" />
        <span class="smalltip">
          <i class="el-icon-info" />
          不使用app支付可不填
        </span>
      </el-form-item>
      <el-form-item label="微信小程序appid">
        <el-button type="text" @click="saveAndGo('/mobile/wxminiprogram/basic')">前往配置页>></el-button>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="onSubmit('form')">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { setConfig } from '@/api/configuration'

export default {
  data() {
    return {
      infoLoading: true,
      form: {
        payment_wechat_mchid: '',
        payment_wechat_key: '',
        payment_wechat_appid: '',
        payment_wechat_appsecret: '',
        payment_wechat_app_appid: '',
        payment_wechat_app_app_appsecret: ''
      }
    }
  },
  mounted() {},
  created() {
    this.fetchInfo()
  },
  methods: {
    fetchInfo() {
      this.infoLoading = true
      const param = {}
      setConfig(param, 'get')
        .then(response => {
          const {
            payment_wechat_mchid,
            payment_wechat_key,
            payment_wechat_appid,
            payment_wechat_appsecret,
            payment_wechat_app_appid,
            payment_wechat_app_app_appsecret
          } = { ...response.data }
          this.form = {
            payment_wechat_mchid,
            payment_wechat_key,
            payment_wechat_appid,
            payment_wechat_appsecret,
            payment_wechat_app_appid,
            payment_wechat_app_app_appsecret
          }
          this.infoLoading = false
        })
        .catch(() => {})
    },
    onSubmit(formName, path = false) {
      const insertData = { ...this.form }
      this.$refs[formName].validate(valid => {
        if (valid) {
          setConfig(insertData)
            .then(response => {
              if (path !== false){
                this.$router.push(path)
                return false
              }
              this.$message.success(response.message)
              return true
            })
            .catch(() => {})
        } else {
          return false
        }
      })
    },
    saveAndGo(path){
      this.onSubmit('form', path)
    }
  }
}
</script>
